<template>
    
    <!-- 组件的模板 --> 
    <div class="page statistics show-tabbar form-report" v-if="isShowTeam">
        <div class="page empty-page" v-show="isteam && list_team && list_team.length == 0 ">
            <div class="mod-empty">
                <div class="icon icon_1"></div>
                <p class="tip">{{permissionsinfo}}</p>
            </div>
        </div>
        <div class="page empty-page" v-show="!isteam && list_personal && list_personal.length == 0">
            <div class="mod-empty">
                <div class="icon icon_1"></div>
                <p class="tip">暂无我提交的表单</p>
            </div>
        </div>
        <ul class="form-list yf-formtjindex-list" v-if="isteam">
            <li v-for="list in list_team" >
                <div class="formlist-count" @click="gotoMemberSubmitDetail(list.id,list.title)">
                    <span>{{list.today_cnt}}</span>
                    <p>今日新增(份)</p>
                </div>
                <div class="formlist-edit">
                    <h3 @click="gotoFormPreview(list.id)">{{list.title}}</h3>
                    <p class="statistical"><span>今日上报</span><span>{{list.today_user_cnt}}人</span></p>
                    <p v-if="list.status === '-1' " class="status end">已暂停</p>
                    <p v-if="list.status === '1' ||  list.status === '0' " class="status ongoing">进行中</p>
                    <p v-if="list.status === '5' " class="status end">已结束</p>
                    <div class="edit" v-if="showeidt">
                        <span @click="gotoEditform(list.id, $index, list.status)">编辑</span>
                        <span @click="gotoSetupforminfo(list.id,$index)">设置</span>
                        <!-- <span @click="shareForm(list.id)">分享</span> -->
                        <span @click="changeStatus($index, list.id, list.status)" v-bind:class="[ list.status === '0' || list.status === '5' ? 'notclick' : '' ]">{{ list.status === '1' || list.status === '0' || list.status === '5' ? '暂停' : '开启' }}</span>
                    </div>
                </div>
            </li>
        </ul>
        <!-- <ul class="data-list_2" v-if="!isteam">
            <li class="com-border-bottom" v-for="list in list_personal" @click="gotoFormTjPersonal(list.id)">
                <a class="touch" href="javascript:;">
                    <h2>{{list.title}}</h2>
                    <span class="time">{{list.submit_time}}</span>
                </a>
            </li>
        </ul> -->
        <ul class="form-list_2" v-if="!isteam">
            <li v-for="list in list_personal" @click="gotoFormTjPersonal(list.id)">
                <a class="bg-color touch flex" href="javascript:;">
                <span class="bg_sprite icon"></span>
                <div class="info">
                    <h3>{{list.title}}</h3>
                    <p>{{list.submit_time}}</p>
                </div>
                </a>
            </li>
        </ul>
    </div>
    <div class="page statistics show-navbar show-tabbar pad_top_0" v-if="!isShowTeam">
        <empty :show-empty="list_personal && list_personal.length == 0" :msg="'暂无我提交的表单'"></empty>
        <ul class="form-list_2">
            <li v-for="list in list_personal" @click="gotoFormTjPersonal(list.id)">
                <a class="bg-color touch flex" href="javascript:;">
                    <span class="bg_sprite icon"></span>
                    <div class="info">
                        <h3>{{list.title}}</h3>
                        <p>{{list.submit_time}}</p>
                    </div>
                </a>
            </li>
        </ul>
    </div>
</template>
<script>
    /**
     *  引入工具类库 或 Native API
     *  example : import widget from 'gapi/widget'
     */
    import * as util from 'js/util.js'
    import socialApi from 'clientApi/socialApi.js'
    import cache from 'gapi/cache.js'
    import commonPage from 'clientApi/commonPage.js'
    import widget from 'gapi/widget.js'
    import KCApiApp from 'clientApi/KCApiApp.js'
    /**
     *  引入组件 以及 组件接口(Action)
     *  example : import Somecomponents from 'components/Somecomponents/Somecomponents'
     */
    import Empty from 'components/Empty/Empty'
    /**
     *  组件实例对象
     */
    export default {
        components: {
            Empty
        },
        data: function(){
            return{
                isteam: true,
                list_team: '',
                permissionsinfo:'空空如也，马上去创建表单吧！',
                list_personal: '', //[]
                user_info: {},
                isShowTeam: false,
                page_no: 1,
                page_size: 15,
                status:true,
                permissions:false,
                pageEnd: false,
                showeidt:false,
                projectname:'',
                formInfo:{},
            }
        },
        vuex: {
            getters: {

            },
            actions: {

            }
        },
        methods: {
            /**
             * 初始化
             * @method init
             * @public
             * @return {Null} void
             */
            init (){
                this.user_info.team_id = util.getQueryString('team_id') || util.getUrddataString('team_id')
                this.user_info.project_id = util.getQueryString('project_id') || util.getUrddataString('project_id')
                this.identity_id = util.getQueryString('identity_id') || util.getUrddataString('identity_id')
                // this.identity_id = 'team'
                if(this.identity_id == 'team'){
                    document.title = '数据收集';
                    this.changenav()
                }else{
                    document.title = '数据上报记录'
                    this.changenav2()
                }
                cache.setCache('user_info', {
                    team_id: this.user_info.team_id,
                    project_id: this.user_info.project_id
                }, 100000)
                cache.setCache('KqCacheMessge', {
                    team_id: this.user_info.team_id,
                    project_id: this.user_info.project_id
                }, 100000)
                //获取权限信息
                util.ajax({
                    url: '/permission/application',
                    type: 'GET',
                    data: {
                        team_id: this.user_info.team_id,
                        project_id: this.user_info.project_id,
                        application_id: 3
                    },
                    timeout: 10000,
                    success: (res) => {
                        if(res.errno == 0){
                            cache.setCache('permission_info', res.data, 100000)
                            //判断显示团队统计
                            res.data.forEach((obj) => {
                                ////form_stat_team 表单统计-团队
                                if(obj.id_name === 'form_stat_team'){
                                    this.isShowTeam = true;
                                }
                                if(obj.id_name === 'form_setting' && this.identity_id == 'team'){ 
                                    this.formInfo.isShowUse = -5;
                                    commonPage.updateTitleBar({
                                        rightDisplay: true,            // 是否显示右按钮
                                        rightText: '创建表单',            // 右按钮文案
                                        rightAction: 'saveResume'    // 点击右按钮的文案（即回调函数名称）
                                    });
                                    window.saveResume = this.gotoSelectTemp;
                                    this.permissionsinfo = '空空如也，马上去创建表单吧';
                                    this.showeidt = true;
                                }
                            })
                            if(this.isShowTeam){
                                this.getTeamList()
                            }else{
                                this.getPersonList()
                            }
                        }else{
                            widget.toast(res.errmsg)
                        }
                        commonPage.loadPageStatus('loadSuccess')
                    },
                    error: (xhr, status) => {
                        widget.toast('服务器异常')
                        commonPage.loadPageStatus('loadSuccess')
                        // error
                    },
                    noNetwork: () => {
                        // 无网络的回调
                        widget.toast('网络连接失败，请检查网络')
                        commonPage.loadPageStatus('loadSuccess')
                    }
                })
                
                window.loadData = () => {
                    if(this.identity_id == 'team'){
                        document.title = '数据收集';
                        this.getTeamList()
                    }else{
                        document.title = '数据上报记录'
                        this.getPersonList()
                    }
                }
                let that = this
                this.$nextTick(function(){
                    let scrollTop = sessionStorage.getItem('scrollTop');
                    if(scrollTop){
                        document.body.scrollTop = scrollTop;
                    }
                });
                $(window).on('scroll',function(){
                    if(!that.isteam){
                        var scrollTop = $(this).scrollTop();
                        var scrollHeight = $(document).height();
                        var windowHeight = $(this).height();
                        if(scrollTop + windowHeight == scrollHeight){
                            if(!that.pageEnd){
                                commonPage.loadPageStatus('loading')
                                that.page_no ++
                                // alert(page_no)
                                util.ajax({
                                    url: '/form/user/report/list',
                                    type: 'GET',
                                    data: {
                                        team_id: that.user_info.team_id,
                                        project_id: that.user_info.project_id,
                                        page_no: that.page_no,
                                        page_size: that.page_size
                                    },
                                    timeout: 10000,
                                    success: (data) => {
                                        // success
                                        // alert(JSON.stringify(data))
                                        if(data.errno == 0){
                                            if(data.data.length > 0 && data.data.length < that.page_size){
                                                that.pageEnd = true
                                            }else if(data.data.length == 0){
                                                // widget.toast('已加载全部')
                                                that.pageEnd = true
                                            }
                                            that.list_personal = that.list_personal.concat(data.data)
                                        }else{
                                            // widget.toast('服务器异常')
                                            widget.toast(data.errmsg)
                                            // alert(JSON.stringify(data))
                                        }
                                        commonPage.loadPageStatus('loadSuccess')
                                    },
                                    error: (xhr, status) => {
                                        // alert(JSON.stringify(data))
                                        widget.toast('服务器异常')
                                        commonPage.loadPageStatus('loadSuccess')
                                        // error
                                    },
                                    noNetwork: () => {
                                        // 无网络的回调
                                        widget.toast('网络连接失败，请检查网络')
                                        commonPage.loadPageStatus('loadSuccess')
                                    }
                                })
                            }else{
                                // widget.toast('已加载全部')
                            }
                            
                        }
                    }
                    
                })  
            },
            changenav (){
                if(!this.isteam){
                    this.isteam = !this.isteam
                    this.getTeamList()
                }
            },
            changenav2 (){
                if(this.isteam){
                    this.isteam = !this.isteam
                    this.getPersonList()
                }
            },
            gotoMemberSubmitDetail (id, title){
                //埋点
                let statisticsLog_data = {
                    event_id: '数据收集-查看今日新增',
                    // event_label: data.event_label,(预留，现在不用传此参数)
                    event_param: {
                        ca_from: 'new',
                        project_id: this.user_info.project_id,
                    }
                }
                // alert(JSON.stringify(statisticsLog_data))
                KCApiApp.statisticsLog(statisticsLog_data)

                let project_name = this.projectname,
                    encodeStr = encodeURIComponent(`form_id=${id}&form_title=${title}&project_name=${project_name}`)
                location.href = `dmsaas://MemberSubmitDetail?urdData=${encodeStr}`
            },
            gotoFormTjPersonal (id){
                let encodeStr = encodeURIComponent(`type=person&record_id=${id}`)
                location.href = `dmsaas://FormTjDetail?urdData=${encodeStr}`
            },
            getTeamList (){
                commonPage.loadPageStatus('loading')
                util.ajax({
                    url: '/form/team/statistics',
                    type: 'GET',
                    data: {
                        team_id: this.user_info.team_id,
                        project_id: this.user_info.project_id
                    },
                    timeout: 10000,
                    success: (data) => {
                        if(data.errno == 0){
                            this.list_team = data.data.form_data;
                            this.projectname = data.data.project_name || '表单统计';
                        }else{
                            widget.toast(data.errmsg)
                        }
                        commonPage.loadPageStatus('loadSuccess')
                    },
                    error: (xhr, status) => {
                        widget.toast('服务器异常')
                        commonPage.loadPageStatus('loadSuccess')
                    },
                    noNetwork: () => {
                        // 无网络的回调
                        widget.toast('网络连接失败，请检查网络')
                        commonPage.loadPageStatus('loadSuccess')
                    }
                })
            },
            getPersonList (){
                commonPage.loadPageStatus('loading')
                this.pageEnd = false
                this.page_no = 1;
                util.ajax({
                    url: '/form/user/report/list',
                    type: 'GET',
                    data: {
                        team_id: this.user_info.team_id,
                        project_id: this.user_info.project_id,
                        page_no: 1,
                        page_size: this.page_size
                    },
                    timeout: 10000,
                    success: (data) => {
                        // success
                        if(data.errno == 0){
                            this.list_personal = data.data
                        }else{
                            widget.toast(data.errmsg)
                        }
                        commonPage.loadPageStatus('loadSuccess')
                    },
                    error: (xhr, status) => {
                        widget.toast('服务器异常')
                        commonPage.loadPageStatus('loadSuccess')
                        // error
                    },
                    noNetwork: () => {
                        // 无网络的回调
                        widget.toast('网络连接失败，请检查网络')
                        commonPage.loadPageStatus('loadSuccess')
                    }
                })
            },
            gotoFormList (id){
                let encodeStr = encodeURIComponent(`form_id=${id}`)
                location.href = `dmsaas://MemberSubmitDetail?urdData=${encodeStr}`
            },
            gotoEditform (id, index, originalStatus){
                // if(originalStatus !== '-1'){
                //     this.changeStatus(index, id, 1,'notoast')
                // }

                this.getsrolltop()

                //埋点
                let statisticsLog_data = {
                    event_id: '数据收集-编辑',
                    // event_label: data.event_label,(预留，现在不用传此参数)
                    event_param: {
                        ca_from: 'edit',
                        project_id: this.user_info.project_id,
                    }
                }
                // alert(JSON.stringify(statisticsLog_data))
                KCApiApp.statisticsLog(statisticsLog_data)

                let encodeStr = encodeURIComponent(`form_id=${id}&originalStatus=${originalStatus}`)
                location.href = `dmsaas://Editform?urdData=${encodeStr}`
            },
            gotoSetupforminfo (id,index){
                //埋点
                let statisticsLog_data = {
                    event_id: '数据收集-设置',
                    // event_label: data.event_label,(预留，现在不用传此参数)
                    event_param: {
                        ca_from: 'setting',
                        project_id: this.user_info.project_id,
                    }
                }
                // alert(JSON.stringify(statisticsLog_data))
                KCApiApp.statisticsLog(statisticsLog_data)
                //获取设置信息
                //this.changeStatus(index, id, 1)
                let encodeStr = encodeURIComponent(`form_id=${id}&type=2`)
                location.href = `dmsaas://SetupFormInfo?urdData=${encodeStr}`
            },
            changeStatus (index, form_id, status,type){
                //表单状态-5已删除 -1暂停 0-未生效 1-生效 5-已结束
                if(status === '0' || status === '5')return; 
                status = (-status).toString();
                cache.getCache('user_info', (data) => {
                    util.ajax({
                        url: '/form/status',
                        type: 'POST',
                        data: {
                            team_id: data.team_id,
                            project_id: data.project_id,
                            form_id: form_id,
                            status: status
                        },
                        timeout: 10000,
                        success: (data) => {
                            if(data.errno == 0){
                                if(type === 'notoast') return;
                                widget.toast(status === '1' ? '表单已开启' : '表单已暂停');
                                if(status == 1){
                                    //埋点
                                    let statisticsLog_data = {
                                        event_id: '数据收集-开启',
                                        // event_label: data.event_label,(预留，现在不用传此参数)
                                        event_param: {
                                            ca_from: 'start',
                                            project_id: this.user_info.project_id,
                                        }
                                    }
                                    // alert(JSON.stringify(statisticsLog_data))
                                    KCApiApp.statisticsLog(statisticsLog_data)
                                }else if(status == -1){
                                    //埋点
                                    let statisticsLog_data = {
                                        event_id: '数据收集-暂停',
                                        // event_label: data.event_label,(预留，现在不用传此参数)
                                        event_param: {
                                            ca_from: 'pause',
                                            project_id: this.user_info.project_id,
                                        }
                                    }
                                    // alert(JSON.stringify(statisticsLog_data))
                                    KCApiApp.statisticsLog(statisticsLog_data)
                                }
                                this.list_team[index].status = status;
                            }else if(data.errno == 15011){
                                widget.toast(data.errmsg)
                                this.form_list[index].status = '-1';
                            }else{
                                // widget.toast('服务器异常')
                                widget.toast(data.errmsg)
                                // alert(JSON.stringify(data))
                            }
                        },
                        error: (xhr, status) => {
                            // error
                            widget.toast('服务器异常')
                        },
                        noNetwork: () => {
                            // 无网络的回调
                            widget.toast('网络连接失败，请检查网络')
                            this.form_list[index].status = -status
                        }
                    })
                })
            },
            //暂去分享功能
            // shareForm (id){
            //     // commonPage.loadPageStatus('loading')
            //     util.ajax({
            //         url: '/form/info/share',
            //         data: {
            //             team_id:this.user_info.team_id,
            //             project_id: this.user_info.project_id,
            //             form_id: id
            //         },
            //         success: (data) => {
            //             // alert(JSON.stringify(data.data.logo))
            //             if(data.errno==0){
            //                 //埋点
            //                 let statisticsLog_data = {
            //                     event_id: '数据收集-分享',
            //                     // event_label: data.event_label,(预留，现在不用传此参数)
            //                     event_param: {
            //                         ca_from: 'share',
            //                         project_id: this.user_info.project_id,
            //                     }
            //                 }
            //                 // alert(JSON.stringify(statisticsLog_data))
            //                 KCApiApp.statisticsLog(statisticsLog_data)

            //                 let params = `?team_id=`+this.user_info.team_id + "&project_id="+this.user_info.project_id + "&form_id="+id + '&report_title=' + data.data.title;

            //                 let shareHref = data.data.url +params;
            //                 // alert(shareHref)
            //                 socialApi.shareKQTask({
            //                     title: data.data.title,
            //                     text: data.data.text,
            //                     logo: data.data.logo,
            //                     url: shareHref
            //                 })
            //             }else{
            //               // widget.toast('服务器异常')
            //               widget.toast(data.errmsg)
            //             }
            //             // commonPage.loadPageStatus('loadSuccess')
            //         },
            //         error: (xhr, status) => {
            //               widget.toast('服务器异常')
            //              // commonPage.loadPageStatus('loadSuccess')
            //         },
            //         noNetwork: () => {
            //             widget.toast('网络连接失败，请检查网络')
            //         }
            //     })
            // },            
            gotoSelectTemp (){
                //埋点
                let statisticsLog_data = {
                    event_id: '数据收集-创建表单',
                    // event_label: data.event_label,(预留，现在不用传此参数)
                    event_param: {
                        ca_from: 'createform',
                        project_id: this.user_info.project_id,
                    }
                }
                // alert(JSON.stringify(statisticsLog_data))
                KCApiApp.statisticsLog(statisticsLog_data)

                location.href = `dmsaas://SelectFormTemp`
            },
            gotoFormPreview (form_id){
                util.ajax({
                    url: '/form/info',
                    type: 'GET',
                    data: {
                        team_id: this.user_info.team_id,
                        project_id: this.user_info.project_id,
                        form_id: form_id
                    },
                    timeout: 10000,
                    success: (data) => {
                        // success
                        // alert(JSON.stringify(data))
                        if(data.errno == 0){
                            this.formInfo.title = data.data.title;
                            this.formInfo.desc = data.data.desc;
                            this.formInfo.form_id = form_id;
                            this.formInfo.format_info = data.data.format_info;
                            // alert(JSON.stringify(data))
                            //cache.setCache('formInfo', this.formInfo, 10000)
                            //setTimeout(this.swiped,100)
                            let temp_info = JSON.stringify(this.formInfo)
                            let encodeStr = encodeURIComponent(`temp_info=${temp_info}`)
                            location.href = `dmsaas://FormTempPreview?urdData=${encodeStr}`
                        }else{
                            // widget.toast('服务器异常')
                            widget.toast(data.errmsg)
                        }
                        
                    },
                    error: (xhr, status) => {
                        // error
                        widget.toast('服务器异常')
                    },
                    noNetwork: () => {
                        // 无网络的回调
                        widget.toast('网络连接失败，请检查网络')
                    }
                })
            },
            getsrolltop (form_id){
                let scrollTop = document.body.scrollTop;
                sessionStorage.setItem('scrollTop', scrollTop);            
            }
        },
        created(){
            this.init();
            let type = util.getUrddataString('type');
        },
        
    }

    
</script>

<!-- CSS 样式 -->
<style src="../../node_modules/weui/dist/style/weui.css"></style>
<style src="../../static/css/common.css"></style>
<style src="../../static/css/form-report.css"></style>
<style type="text/css">
    html,body{
        height: 100%;
    }
    .com-border-bottom::after {
        left:12px;
    }
    .com-border-bottom:last-child::after {
        left:0px;
    }
    .yf-formtjindex-list li .formlist-edit .edit span{
        color: #666;
    }
    .right-dq{
        text-align: right;
    }
    div.page.pad_top_0{
        padding-top: 0px;
    }
    @media (max-width: 325px) {
        .yf-formtjindex-list li .formlist-edit{
            padding-left: 8px; 
        }
        .yf-formtjindex-list li .formlist-count{
            padding: 0 4px;
        }
        .yf-formtjindex-list li .formlist-edit .edit span{
            /*width: 49px;*/
            height: 22px;
            line-height: 22px;
            margin-right:7;
            padding: 4px 23px;
        }
        .yf-formtjindex-list li .formlist-edit .edit span + span{
            margin-left: 2px;
        }
    }
    /*@media screen and (max-width: 320px) {
        .yf-formtjindex-list li .formlist-edit .edit span + span{
            margin-left: 2px;
        }
    }*/

</style>


